/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2021. All rights reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cn.kongnannan.example;

import cn.kongnannan.circularavatar.JoinBitmaps;
import cn.kongnannan.circularavatar.JoinLayout;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Image;
import ohos.agp.components.element.PixelMapElement;
import ohos.media.image.PixelMap;

import java.util.ArrayList;

/**
 * PixelMapAblity
 */
public class PixelMapAblity extends Ability {
    private ArrayList<PixelMap> mBmps = new ArrayList<PixelMap>();
    private Image mImageView1;
    private Image mImageView2;
    private Image mImageView3;
    private Image mImageView4;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ablity_bitmap);
        initValues();
    }

    private void initValues() {
        mImageView1 = (Image) findComponentById(ResourceTable.Id_imageView1);
        mImageView2 = (Image) findComponentById(ResourceTable.Id_imageView2);
        mImageView3 = (Image) findComponentById(ResourceTable.Id_imageView3);
        mImageView4 = (Image) findComponentById(ResourceTable.Id_imageView4);
        PixelMapElement avatar1 = ResUtil.getPixelMapDrawable(getContext(), ResourceTable.Media_headshow1);
        PixelMapElement avatar2 = ResUtil.getPixelMapDrawable(getContext(), ResourceTable.Media_headshow2);
        PixelMapElement avatar3 = ResUtil.getPixelMapDrawable(getContext(), ResourceTable.Media_headshow3);

        mBmps.add(avatar1.getPixelMap());
        mBmps.add(avatar2.getPixelMap());
        mBmps.add(avatar3.getPixelMap());
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            mImageView1.setPixelMap(JoinBitmaps.createBitmap(mImageView1.getWidth(),
                    mImageView1.getHeight(), mBmps));

            int count = Math.min(mBmps.size(), JoinLayout.max());
            float[] size2 = size2(count);

            mImageView2.setPixelMap(JoinBitmaps.createBitmap(mImageView2.getWidth(),
                    mImageView2.getHeight(), mBmps, count, size2));

            float[] size3 = size3(count);
            mImageView3.setPixelMap(JoinBitmaps.createBitmap(mImageView3.getWidth(),
                    mImageView3.getHeight(), mBmps, count, size3));

            mImageView4.setPixelMap(JoinBitmaps.createBitmap(mImageView4.getWidth(),
                    mImageView4.getHeight(), mBmps, count, size2, 0.3f));
        }
    }

    private static final float[][] sizes3 = {new float[]{0.9f, 0.9f},
            new float[]{0.5f, 0.65f}, new float[]{0.45f, 0.9f},
            new float[]{0.45f, 0.91f}, new float[]{0.38f, 0.80f}};

    /**
     * size3
     *
     * @param count integer
     * @return value
     */
    public static float[] size3(int count) {
        return count > 0 && count <= sizes3.length ? sizes3[count - 1] : null;
    }

    private static final float[][] sizes2 = {new float[]{0.9f, 0.9f},
            new float[]{0.5f, 0.65f}, new float[]{0.35f, 0.8f},
            new float[]{0.45f, 0.91f}, new float[]{0.38f, 0.80f}};

    /**
     * size2
     *
     * @param count integer
     * @return count
     */
    public static float[] size2(int count) {
        return count > 0 && count <= sizes2.length ? sizes2[count - 1] : null;
    }
}
